package systemActions;

import usersObjects.*;

/**
 * Created with IntelliJ IDEA.
 * User: xpk
 * Date: 11/12/13
 * Time: 4:08 PM
 * To change this template use File | Settings | File Templates.
 */
public class UsersRegistration extends SystemActionsDecorator {
    private SystemAction currentSystemAction;

    public UsersRegistration(SystemAction currentSystemAction) {
        this.currentSystemAction = currentSystemAction;
    }

    @Override
    public void executeAction(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        identificationOfUser(currentStorageOfUser, currentUserCertificate);
    }

    private void identificationOfUser(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        if (!currentStorageOfUser.hasUser(currentUserCertificate.getUserLogin())) {
            currentStorageOfUser.add(currentUserCertificate);
        }

        authenticationOfUser(currentStorageOfUser, currentUserCertificate);
    }

    private void authenticationOfUser(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        if (currentStorageOfUser.checkUserPassword(currentUserCertificate)) {
            System.out.println("You are in the system");
            authorizationOfUser(currentStorageOfUser, currentUserCertificate);
        } else {
            System.out.println("You have typed a wrong password for a user, please try again");
        }
    }

    private void authorizationOfUser(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        UserBehavior currentUserBehavior;
        if (currentUserCertificate.getUserLogin() != "admin") {
            currentUserBehavior = UserBehaviorsFactory.getUserBehavior("UserTesting");
            currentStorageOfUser.setUserBehavior(currentUserBehavior, currentUserCertificate);
            currentUserBehavior = UserBehaviorsFactory.getUserBehavior("UserStatistic");
            currentStorageOfUser.setUserBehavior(currentUserBehavior, currentUserCertificate);
            currentUserBehavior = UserBehaviorsFactory.getUserBehavior("UserHistory");
            currentStorageOfUser.setUserBehavior(currentUserBehavior, currentUserCertificate);
            currentUserBehavior = UserBehaviorsFactory.getUserBehavior("UserSave");
            currentStorageOfUser.setUserBehavior(currentUserBehavior, currentUserCertificate);
        }

        currentSystemAction.executeAction(currentStorageOfUser, currentUserCertificate);
    }
}
